package com.android.kotlincoroutinemvvmdemo
import android.animation.Keyframe
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import com.android.kotlincoroutinemvvmdemo.base.BaseActivity
import com.android.kotlincoroutinemvvmdemo.databinding.ActivityFlowDemoBinding
import com.android.kotlincoroutinemvvmdemo.hook.HookListener
import com.android.kotlincoroutinemvvmdemo.hook.HookToast
import com.android.kotlincoroutinemvvmdemo.view.AnimationView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

class FlowDemoActivity : BaseActivity<ActivityFlowDemoBinding>(), AnimationView.IUpdateCallback {

    override fun getLayoutId() = R.layout.activity_flow_demo

    override fun init(savedInstanceState: Bundle?) {
        //禁止截屏标识
        window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)

        //flowOn操作符可以使用多次,切换线程只会影响它上游的逻辑.  collect在主线程中执行
//        launch {
//            flow {
//                val value = getValue()
//                emit(value)
//            }.flowOn(Dispatchers.IO)
//                .onStart {
//                    Log.d(TAG,"onStat-----33-----${Thread.currentThread().name}")
//                }
//                .map {
//                    it * 5
//                    mBinding.tvTestFlow.text = "value:${it * 5}"
//                    Log.d(TAG,"00-${Thread.currentThread().name}-00")
//                }
//                .flowOn(Dispatchers.Main)
//                .collect {
//                    Log.d(TAG, "value:${it}")
//                    Log.d(TAG,"11-${Thread.currentThread().name}-11")
//                }
//        }

        launch {
            flowOf(1, 23, 456, 789)
                .onEach { value ->
                    Log.d(TAG, "value0:${value}")
                    //Log.d(TAG, "name--${Thread.currentThread().name}--name")
                    flow {
                        emit(value)
                    }
                }
                .filter {
                    it > 250
                }
                .onEach {
                    Log.d(TAG, "value1:${it}")
                }
                .launchIn(CoroutineScope(Dispatchers.Main))
        }

        mBinding.animationView1.startSwipe()

        mBinding.arcProgressView.startSwipe()

        mBinding.arcProgressView2.startSwipe()

        mBinding.radarView.startSwipe()

        mBinding.radarView2.startSwipe()

        mBinding.rotateView.startRotateX()

        mBinding.sevenCircleProgressView.startAnimation()

        mBinding.rotateView2.startRotateY()

        mBinding.startAnimation.setOnClickListener {
            mBinding.ellipticView.startAnimation()

            mBinding.curveBezierView.startAnimation()

            //startKeyFrame()

            shakeAnimation(mBinding.tvTestFlow, 1.0F)
        }

        mBinding.btnHook.setOnClickListener {
            HookToast.show(this@FlowDemoActivity, "我数123,木头人.", Toast.LENGTH_SHORT)
        }
        HookListener.registerListener(this@FlowDemoActivity, mBinding.btnHook)

        mBinding.rectProgressRotateView.startAnimation()
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding.rectProgressRotateView.stopAnimation()
    }

    override fun onItemAnimationFinished(itemIndex: Int) {

    }

    override fun onAllAnimationFinished() {

    }

    private suspend fun getValue(): Int {
        delay(5000)
        return 10
    }

    //来回移动
    private fun startKeyFrame() {
        val key1 = Keyframe.ofFloat(0f, 0f)
        val key2 = Keyframe.ofFloat(0.2f, 600f)
        val key3 = Keyframe.ofFloat(0.5f, 50f)
        val key4 = Keyframe.ofFloat(1f, 300f)

        val holder = PropertyValuesHolder.ofKeyframe("translationX", key1, key2, key3, key4)
        val animation = ObjectAnimator.ofPropertyValuesHolder(mBinding.tvTestFlow, holder)
        animation.repeatMode = ObjectAnimator.REVERSE
        animation.repeatCount = ObjectAnimator.INFINITE
        animation.duration = 3000
        animation.start()
    }

    //抖动
    private fun shakeAnimation(view: View, shakeFactor: Float) {
        val pvhScaleX = PropertyValuesHolder.ofKeyframe(
            View.SCALE_X,
            Keyframe.ofFloat(0F, 1F),
            Keyframe.ofFloat(0.1F, 0.9F),
            Keyframe.ofFloat(0.2F, 0.9F),
            Keyframe.ofFloat(0.3F, 1.1F),
            Keyframe.ofFloat(0.4F, 1.1F),
            Keyframe.ofFloat(0.5F, 1.1F),
            Keyframe.ofFloat(0.6F, 1.1F),
            Keyframe.ofFloat(0.7F, 1.1F),
            Keyframe.ofFloat(0.8F, 1.1F),
            Keyframe.ofFloat(0.9F, 1.1F),
            Keyframe.ofFloat(1F, 1F)
        )

        val pvhScaleY = PropertyValuesHolder.ofKeyframe(
            View.SCALE_Y,
            Keyframe.ofFloat(0F, 1F),
            Keyframe.ofFloat(0.1F, 0.9F),
            Keyframe.ofFloat(0.2F, 0.9F),
            Keyframe.ofFloat(0.3F, 1.1F),
            Keyframe.ofFloat(0.4F, 1.1F),
            Keyframe.ofFloat(0.5F, 1.1F),
            Keyframe.ofFloat(0.6F, 1.1F),
            Keyframe.ofFloat(0.7F, 1.1F),
            Keyframe.ofFloat(0.8F, 1.1F),
            Keyframe.ofFloat(0.9F, 1.1F),
            Keyframe.ofFloat(1F, 1F)
        )

        val pvhRotate = PropertyValuesHolder.ofKeyframe(
            View.ROTATION,
            Keyframe.ofFloat(0F, 0F),
            Keyframe.ofFloat(0.1F, -3F * shakeFactor),
            Keyframe.ofFloat(0.2F, -3F * shakeFactor),
            Keyframe.ofFloat(0.3F, 3F * shakeFactor),
            Keyframe.ofFloat(0.4F, -3F * shakeFactor),
            Keyframe.ofFloat(0.5F, 3F * shakeFactor),
            Keyframe.ofFloat(0.6F, -3F * shakeFactor),
            Keyframe.ofFloat(0.7F, 3F * shakeFactor),
            Keyframe.ofFloat(0.8F, -3F * shakeFactor),
            Keyframe.ofFloat(0.9F, 3F * shakeFactor),
            Keyframe.ofFloat(1F, 0F)
        )

        val animator =
            ObjectAnimator
                .ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY, pvhRotate)
                .setDuration(1000)
        animator.repeatCount = ValueAnimator.INFINITE
        animator.start()
    }

    companion object {
        const val TAG = "FlowDemoActivity"
    }

}